import { request } from '@/plugins/http';

// 获取权限
export const apiSystemPermissions = async (
  query?: any,
): Promise<API.SystemPermission[]> => {
  const resp = await request('/admin/system/permissions', {
    params: query,
  });
  return resp.data.items;
};

export const apiRole = {
  // 获取角色列表
  getList: async (query?: any): Promise<API.List<API.Role>> => {
    return await request('/admin/role/list', {
      params: query,
    });
  },
  // 获取角色的用户
  getUsers: async (id: number, query?: any): Promise<API.List<API.Users>> => {
    return await request(`/admin/role/${id}/users`, {
      params: query,
    });
  },
  // 将用户移出角色
  deleteUsers: async (id: number, users: number[]): Promise<void> => {
    return await request(`/admin/role/${id}/users`, {
      method: 'delete',
      data: {
        users,
      },
    });
  },
  // 获取角色的权限
  getPermissions: async (
    id: number,
    query?: any,
  ): Promise<API.RolePermission[]> => {
    return await request(`/admin/role/${id}/permissions`, {
      params: query,
    });
  },
  // 获取角色信息
  getInfo: async (id: number): Promise<API.Role> => {
    const resp = await request(`/admin/role/${id}`);
    return resp.data;
  },
  // 修改角色信息
  editInfo: async (id: number, data: API.Role): Promise<API.Role> => {
    const resp = await request(`/admin/role/${id}`, {
      method: 'post',
      data,
    });
    return resp.data;
  },
  // 为角色添加权限
  addPermissions: async (
    ids: number[],
    permissions: string[],
  ): Promise<void> => {
    return await request(`/admin/role/${ids.join(',')}/permissions`, {
      method: 'post',
      data: {
        permissions,
      },
    });
  },
  // 为角色删除权限
  deletePermissions: async (
    ids: number[],
    permissions: string[],
  ): Promise<void> => {
    return await request(`/admin/role/${ids.join(',')}/permissions`, {
      method: 'delete',
      data: {
        permissions,
      },
    });
  },
  // 删除角色
  delete: async (ids: number[]): Promise<void> => {
    return await request(`/admin/role/${ids.join(',')}`, { method: 'delete' });
  },
};
